<?php
namespace app\staff\controller;

use think\Request;
use think\Db;
use think\Session;
use think\Controller;
use think\Config;
use Godok\Org\Auth;
use think\Log;

class Shipping extends Controller
{	
	//退货展示页面
	function index(){
		$order_goods_id = request()->param('order_goods_id');
		$order = Db::name('order_goods')->where('order_goods_id',$order_goods_id)->find();
		if($order){
			$this->success('success','',$order);
		}else{
			$this->error('error');
		}
	}

	//退货
	function refund(){
		$order_id = Request()->param('order_id');
		$order_goods_id = Request()->param('order_goods_id');
		$nums = Request()->param('nums');
		$remarks = Request()->param('remarks');
		$employee_id = Request()->param('employee_id');

		$employee = Db::name('employee')->where('id',$employee_id)->find();

		$res = Db::name('order')->where('order_id',$order_id)->find();
		$shipping = Db::name('shipping')->where('order_id',$order_id)->find();
		$is_refund = Db::name('shipping_refund')->where('order_id',$order_id)->find();
		Db::startTrans();
		if(!$is_refund){
			$arr = [
				'order_id' 		=> $order_id,			
				'order_no' 		=> $res['order_no'],			
				'buyer_id' 		=> $res['user_id'],			
				'buyer_name' 	=> $res['user_name'],			
				'agent_id' 		=> $res['agent_id'],			
				'agent_name' 	=> $res['agent_name'],			
				'delivery_id' 	=> $shipping['delivery_id'],			
				'delivery_name' => $shipping['delivery_name'],			
				'create_time' 	=> date('Y-m-d H:i:s')			
			];
			Db::name('shipping_refund')->insert($arr);	
		}
		$goods = Db::name('order_goods')->where('order_goods_id',$order_goods_id)->find();

		$money = $nums*$goods['price'];
		$_remark = "商品（{$goods['brand_name']}）{$goods['goods_name']}退货数量：{$nums}";
		$map = [
			'order_id'			=> $order_id,
			'user_id' 			=> $res['user_id'],
			'act_time' 			=> date('Y-m-d H:i:s'),
			'op_uid' 			=> $employee_id,
			'op_uname'			=> $employee['username'],
			'order_status' 		=> $res['order_status'],
			'bop_order_status'	=> $res['order_status'],
			'op_model'			=> 'employee',
			'act_remark'     	=> '订单部分商品已退货，商品已支付金额将在3个工作日内退回||'.$_remark
		];
		$re = Db::name('order_action')->insert($map);
		if(!$re){
			Db::rollback();
			$this->error('退货失败');
		}

		$result = [
			'og_id' => $order_goods_id,
			'order_id' => $order_id,
			'order_no' => $res['order_no'],
			'goods_id' => $goods['goods_id'],
			'specval_id' => $goods['goods_specval_id'],
			'goods_name' => $goods['goods_name'],
			'rg_time' => date('Y-m-d H:i:s'),
			'rg_num' => $nums,
			'rg_remark' => $remarks,
			'return_money' => $goods['price']
		];
		$obj = Db::name('order_goods_return')->insertGetId($result);
		if(!$obj){
			Db::rollback();
			$this->error('退货失败');
		}

		$info = [
			'create_time'	=>	date('Y-m-d H:i:s'),
			'remark'		=> 	$remarks,
			'money'			=>	$money,
			'order_id'		=>	$order_id,
			'audit_type'	=>	1,
		];
		$add_audit = Db::name('order_refund_audit')->insertGetId($info);
		if(!$add_audit){
			Db::rollback();
			$this->error('退货失败');
		}

		$edit_return = Db::name('order_goods_return')->where('rg_id',$obj)->update(['audit_id'=>$add_audit]);
		if(!$edit_return){
			Db::rollback();
			$this->error('退货失败');
		}
		$edit = Db::name('order_goods')->where('order_goods_id',$order_goods_id)->setDec('num',$nums);
		if(!$edit){
			Db::rollback();
			$this->error('退货失败');
		}
		Db::commit();
		$this->success('退货成功');
	}

	//配送完成
	function draw(){

/*		if ( Request::instance()->isPost() ) {
			$param = request()->param();
			$path = $_SERVER['DOCUMENT_ROOT'].'/';

			$realpath = str_replace('/', DS, realpath($path).'/');

			//匹配出图片的格式
			if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $param['data'], $result)){
				$type = $result[2];
				if(!file_exists($realpath))
				{
					//检查是否有该文件夹，如果没有就创建，并给予最高权限
					mkdir($realpath, 0700);
				}
				$savename = time().".{$type}";
				if (file_put_contents($realpath.$savename, base64_decode(str_replace($result[1], '', $param['data'])))){
					return ['code'=>1,'msg'=>'','data'=> request()->domain().'/'.$savename];
				}else{
					return ['code'=>0,'msg'=>'error','data'=> ''];
				}
			}

		}

*/

		$order_id = request()->param('order_id');
		$addr=Request::instance()->post('addr');				//获取签名花板图片保存路径
		$employee_id = Request()->param('employee_id');

		$employee = Db::name('employee')->where('id',$employee_id)->find();

		if($addr){
			$path = $_SERVER['DOCUMENT_ROOT'].'/';
			$realpath = str_replace('/', DS, realpath($path).'/');
			//匹配出图片的格式
			if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $addr, $result)){
				$type = $result[2];
				if(!file_exists($realpath))
				{
					//检查是否有该文件夹，如果没有就创建，并给予最高权限
					mkdir($realpath, 0700);
				}
				$savename = time().".{$type}";
				if(!file_put_contents($realpath.$savename, base64_decode(str_replace($result[1], '', $addr)))){
					$this->error('图片保存失败');
				}
			}

			$order = Db::name('order')->where('order_id',$order_id)->find();
			if ($order['order_status'] == 100) {
				$this->success('感谢您的签收');
			}
			Db::startTrans();
			$map = [
				'order_id' => $order_id,
				'user_id' => $order['user_id'],
				'act_time' => date('Y-m-d H:i:s'),
				'op_uid' => $employee_id,
				'op_uname' => $employee['username'],
				'order_status' => 100,
				'bop_order_status' => $order['order_status'],
				'op_model' => 'employee'
			];
			$re = Db::name('order_action')->insert($map);
			$time = date('Y-m-d H:i:s');
			$order_edit = Db::name('order')->where('order_id',$order_id)->setField(['order_status'=>100,'signature'=>request()->domain().'/'.$savename,'sign_time'=>$time,'finish_time'=>$time]);
			$shipping_edit = Db::name('shipping')->where('order_id',$order_id)->setField(['shipping_status'=>2,'finish_time'=>$time]);
			if($re && $order_edit && $order_edit){
				Db::commit();
				$this->success('感谢您的签收');
			}else{
				Db::rollback();
				$this->error('操作失败');
			}
		}
	}
}